/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit.ieproject.crawler;

import edu.uci.ics.crawler4j.crawler.CrawlConfig;
import edu.uci.ics.crawler4j.crawler.CrawlController;
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.DefaultCaret;
import uit.ieproject.presentation.MainFrame;
import uit.ieproject.presentation.ProgressDownloadFrame;

/**
 *
 * @author RedDevils
 */
public class MyControllerThread implements Runnable {

    public static CrawlController controller;
    private int numberOfCrawlers = 10;
    private ProgressDownloadFrame processFrame;

    public MyControllerThread(ArrayList<String> arrlistLink) {
        MainFrame.arrlistLink = arrlistLink;
        processFrame = new ProgressDownloadFrame();
        DefaultCaret caret = (DefaultCaret) processFrame.tpStatus.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    }

    @Override
    public void run() {
        String crawlStorageFolder = "/data/crawl/root";
        numberOfCrawlers = 10;

        CrawlConfig config = new CrawlConfig();

        config.setCrawlStorageFolder(crawlStorageFolder);

        config.setPolitenessDelay(1000);

        config.setMaxDepthOfCrawling(1);

        config.setMaxPagesToFetch(1000);

        config.setResumableCrawling(false);

        /*
         * Instantiate the controller for this crawl.
         */
        PageFetcher pageFetcher = new PageFetcher(config);
        RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
        RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
        try {
            controller = new CrawlController(config, pageFetcher, robotstxtServer);
        } catch (Exception ex) {
            Logger.getLogger(MyControllerThread.class.getName()).log(Level.SEVERE, null, ex);
        }

        for (int i = 0; i < MainFrame.arrlistLink.size(); i++) {
            controller.addSeed(MainFrame.arrlistLink.get(i));
        }
        processFrame.setLocationRelativeTo(null); 
        processFrame.setVisible(true);
        controller.start(MyCrawler.class, numberOfCrawlers);
        processFrame.dispose();
        MainFrame.downloadNameButton.setEnabled(true);
    }
}
